package com.senac.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class GerenciamentoEscolaServlet<E> extends HttpServlet {

	private static final long serialVersionUID = 8876356307877761175L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		process(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		process(req, resp);		
	}
	
	protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String acao = req.getParameter("acao");
		
		System.out.println("process: " + acao);
		
		if(acao==null) {
			cadastrar(req, resp);
		} else {
			switch(acao) {
				case "salvar":
					salvar(req, resp);
					break;
				case "editar":
					editar(req, resp);
					break;
				case "remover":
					excluir(req, resp);
					break;
				case "listar":
					listar(req, resp);
					break;
				default:
					cadastrar(req, resp);
			}
		}
	}

	protected abstract void cadastrar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;
	
	protected abstract void salvar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;
	
	protected abstract void editar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;
	
	protected abstract void excluir(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;
	
	protected abstract E mountByRequest(HttpServletRequest req);
	
	protected abstract void listar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;

}
